<template>
    <div class="step-basic-container-template">
        <div class="control-bar">
            <h3>Campaign / Basics</h3>
        </div>
        <div class="container">

            <el-row class="intro">
                <h2>
                    Basics
                </h2>
                <p class="sub-header">
                    Basics
                    Make a good first impression: introduce your campaign objectives and entice people to learn more.
                    This basic
                    information will represent your campaign on your campaign page, on your campaign card, and in
                    searches.
                </p>
            </el-row>
            <el-row class="form-area">
                <div class="form-item">
                    <label class="entreField-label">
                        Campaign Title
                        <span class="entreField-required">*</span>
                    </label>
                    <div class="entreField-sublabel">
                        What is the title of your campaign?
                    </div>
                    <el-input
                            type="input"
                            placeholder=""
                            v-model="campaignTitle">
                    </el-input>

                </div>

                <div class="form-item">
                    <label class="entreField-label">
                        Campaign Tagline
                        <span class="entreField-required">*</span>
                    </label>
                    <div class="entreField-sublabel">
                        Provide a short description that best describes your campaign to your audience.
                    </div>
                    <el-input
                            type="textarea"
                            :autosize="{ minRows: 2, maxRows: 4}"
                            placeholder=""
                            v-model="campaignTagline">
                    </el-input>
                </div>


                <div class="form-item">
                    <label class="entreField-label">
                        Campaign Card Image
                        <span class="entreField-required">*</span>
                    </label>
                    <div class="entreField-sublabel">
                        Upload a square image that represents your campaign.
                    </div>
                    <div class="entreField-sublabel">
                        640 x 640 recommended resolution, 220 x 220 minimum resolution.
                    </div>
                    <el-upload
                            :action="uploadHost"
                            name="picture"
                            list-type="picture-card"
                            :on-preview="handlePictureCardPreview"
                            :on-success="cardImageUploadHandler"
                            :on-remove="handleRemove"
                            :with-credentials="true"
                            :headers="uploadHeaders">
                        <i class="el-icon-plus"></i>
                    </el-upload>
                    <el-dialog :visible.sync="dialogVisible">
                        <img width="100%" :src="dialogImageUrl" alt="">
                    </el-dialog>
                </div>

                <div class="form-item">
                    <label class="entreField-label">
                        Location
                        <span class="entreField-required">*</span>
                    </label>
                    <div class="entreField-sublabel">
                        Choose the location where you are running the campaign. This location will be visible on your
                        campaign page
                        for your audience to see.
                    </div>
                    <el-row :gutter="20">
                        <el-col :span="11">
                            <el-input
                                    type="input"
                                    placeholder="city"
                                    v-model="locationCity">
                            </el-input>
                        </el-col>
                        <el-col :span="11">
                            <el-input
                                    type="input"
                                    placeholder="country"
                                    v-model="locationCountry">
                            </el-input>
                        </el-col>
                    </el-row>

                </div>


                <div class="form-item">
                    <label class="entreField-label">
                        Category
                        <span class="entreField-required">*</span>
                    </label>
                    <div class="entreField-sublabel">
                        To help backers find your campaign, select a category that best represents your project.
                    </div>
                    <el-select v-model="campaignCategory" placeholder="Select a category">
                        <el-option
                                v-for="item in category"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                            <span style="float: left">{{ item.label }}</span>
                            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
                        </el-option>
                    </el-select>
                </div>

                <div class="form-item">
                    <label class="entreField-label">
                        Tags
                        <span class="entreField-required">*</span>
                    </label>

                    <el-tag
                            :key="tag"
                            v-for="tag in campaignTags"
                            closable
                            :disable-transitions="false"
                            @close="handleClose(tag)">
                        {{tag}}
                    </el-tag>
                    <el-input
                            class="input-new-tag"
                            v-if="inputVisible"
                            v-model="inputValue"
                            ref="saveTagInput"
                            size="small"
                            @keyup.enter.native="handleInputConfirm"
                            @blur="handleInputConfirm"
                    >
                    </el-input>
                    <el-button v-else class="button-new-tag" size="small" @click="showInput">+ New Tag</el-button>


                </div>


                <div class="form-item">
                    <label class="entreField-label">
                        Campaign Duration
                        <span class="entreField-required">*</span>
                    </label>
                    <div class="entreField-sublabel">
                        How many days will you be running your campaign for? You can run a campaign for any number of
                        days, with a
                        60 day duration maximum.
                    </div>

                    <el-input-number v-model="campaignDay" @change="handleChange" :min="10" :max="100"
                                     label="描述文字"></el-input-number>

                    <!--<el-date-picker
                            v-model="campaignDuration"
                            style="width: 160px;"
                            type="date"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="选择日期">
                    </el-date-picker>-->

                </div>


            </el-row>
            <el-row class="ope-area">
                <el-button type="primary" @click="toStep2">Save & Continue</el-button>
            </el-row>
        </div>

    </div>
</template>

<script>
    import {getToken, setToken, removeToken} from '@/utils/auth'
    import Constant from '@/env'
    import {mapState, mapMutations} from 'vuex'

    export default {
        name: "Basics",
        data() {
            return {
                campaignTitle: "",
                campaignTagline: "",
                campaignCardImage: "",
                campaignTags: [],
                inputVisible: false,
                inputValue: '',
                campaignCategory: "",
                campaignDay: 10,
                campaignDuration: "",
                locationCity: '',
                locationCountry: '',
                dialogImageUrl: '',
                dialogVisible: false,
                cities: [{
                    value: 'Art',
                    label: 'Art'
                }, {
                    value: 'Education',
                    label: 'Education'
                }, {
                    value: 'Work',
                    label: 'Work'
                }, {
                    value: 'Sports',
                    label: 'Sports'
                }, {
                    value: 'Shenzhen',
                    label: '深圳'
                }, {
                    value: 'Guangzhou',
                    label: '广州'
                }],
                tags: [{
                    value: 'Beijing',
                    label: '北京'
                }, {
                    value: 'Shanghai',
                    label: '上海'
                }, {
                    value: 'Nanjing',
                    label: '南京'
                }, {
                    value: 'Chengdu',
                    label: '成都'
                }, {
                    value: 'Shenzhen',
                    label: '深圳'
                }, {
                    value: 'Guangzhou',
                    label: '广州'
                }],
                category: [{
                    value: 'Art',
                    label: 'Art'
                }, {
                    value: 'Education',
                    label: 'Education'
                }, {
                    value: 'Work',
                    label: 'Work'
                }, {
                    value: 'Sports',
                    label: 'Sports'
                }],
                uploadHeaders: {}
            }
        },
        computed: {
            ...mapState({
                'userId': state => state.user.userId,
                'userName': state => state.user.userName,
                'campaignStore': state => state.campaign.item,
            }),
            uploadHost() {
                return Constant.baseUrl + '/crowd-api/api/image/upload'
            }

        },
        mounted() {
            // 添campaignDuration加上传headers
            this.uploadHeaders["token"] = getToken()
            this.uploadHeaders["userId"] = this.userId
        },
        // 页面进入之前，请求数据
        beforeRouteEnter(to, from, next) {

            next(vm => {
                var campaignItem = vm.$store.state.campaign.item
                vm.campaignTitle = campaignItem.title
                vm.campaignTagline = campaignItem.tagline
                vm.campaignCategory = campaignItem.category
                vm.campaignCardImage = campaignItem.campaignImage
                vm.dialogImageUrl = campaignItem.campaignImage
                vm.locationCity = campaignItem.locationCity
                vm.locationCountry = campaignItem.locationCountry
                vm.campaignTags = campaignItem.tagList
                // vm.dialogVisible = true
            })
        },
        methods: {
            handleRemove(file, fileList) {
            },
            handleChange(val) {
                /* console.log(val)
                 if(val > 0) {
                     this.campaignDuration = new Date().getTime() + 1000 * 3600 * 24 * this.campaignDay
                     console.log(this.campaignDuration)
                 }*/
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            checkAllInput() {


                var checkTitle = this.campaignTitle || ""
                if ("" == checkTitle) {
                    this.$message.error("campaign title can't be empty")
                    return false
                }


                var checkCampaignTagline = this.campaignTagline || ""
                if ("" == checkCampaignTagline) {
                    this.$message.error("campaignTagLine title can't be empty")
                    return false
                }

                if ("" == this.campaignCardImage) {
                    this.$message.error("card image not correct")
                    return false
                }


                var checkCategory = this.campaignCategory || ""
                if ("" == checkCategory) {
                    this.$message.error("campaignCategory not correct")
                    return false
                }

                if ("" == this.locationCity || "" == this.locationCountry) {
                    this.$message.error("location not correct")
                    return false
                }

                var checkTags = this.campaignTags || ""
                if ("" == checkTags) {
                    this.$message.error("campaignTags not correct")
                    return false
                }

                var checkDuration = this.campaignDay || ""
                if ("" == checkDuration) {
                    this.$message.error("campaignDuration not correct")
                    return false
                }

                return true


            },
            toStep2() {

                // check can continue
                var flag = this.checkAllInput()

                if (!flag) {
                    return
                }

                var that = this
                this.$store.dispatch('SAVE_CAMPAIGN_INFO', {
                    'mutationName': 'SET_TITLE',
                    'value': this.campaignTitle
                })
                this.$store.dispatch('SAVE_CAMPAIGN_INFO', {
                    'mutationName': 'SET_TAGLINE',
                    'value': this.campaignTagline
                })
                this.$store.dispatch('SAVE_CAMPAIGN_INFO', {
                    'mutationName': 'SET_CAMPAIGN_IMAGE',
                    'value': this.campaignCardImage
                })
                this.$store.dispatch('SAVE_CAMPAIGN_INFO', {
                    'mutationName': 'SET_LOCATION_CITY',
                    'value': this.locationCity
                })
                this.$store.dispatch('SAVE_CAMPAIGN_INFO', {
                    'mutationName': 'SET_LOCATION_COUNTRY',
                    'value': this.locationCountry
                })
                this.$store.dispatch('SAVE_CAMPAIGN_INFO', {
                    'mutationName': 'SET_CATEGORY',
                    'value': this.campaignCategory
                })

                this.$store.dispatch('SAVE_CAMPAIGN_INFO', {
                    'mutationName': 'SET_TAG',
                    'value': this.campaignTags
                })


                this.campaignDuration = new Date().getTime() + 1000 * 3600 * 24 * this.campaignDay

                this.$store.dispatch('SAVE_CAMPAIGN_INFO', {
                    'mutationName': 'SET_CAMPAIGN_DURATION',
                    'value': this.campaignDuration
                })


                this.$router.push('/crowd/step2');
            },
            // 文件上传成功回调
            cardImageUploadHandler(response, file, fileList) {
                if (0 == response.code) {
                    this.campaignCardImage = response.imageUrl
                }
            },
            handleClose(tag) {
                this.campaignTags.splice(this.campaignTags.indexOf(tag), 1);
            },

            showInput() {
                this.inputVisible = true;
                this.$nextTick(_ => {
                    this.$refs.saveTagInput.$refs.input.focus();
                });
            },

            handleInputConfirm() {
                let inputValue = this.inputValue;
                if (inputValue) {
                    this.campaignTags.push(inputValue);
                }
                this.inputVisible = false;
                this.inputValue = '';
            }
        }
    }
</script>

<style lang="scss" scoped>
    .step-basic-container-template {
        .control-bar {
            display: flex;
            height: 81px;
            border-bottom: 1px solid rgb(200, 200, 200);
            align-items: center;
            padding: 40px;
            h3 {
                color: #2a2a2a;
                font-weight: bold;
            }
        }
        .container {
            margin-left: 26px;
            width: 950px;
            padding: 0 40px;

            .intro {
                .sub-header {
                    ont-family: "Benton Sans", "Helvetica", "sans-serif";
                    font-size: 16px;
                    font-weight: normal;
                    letter-spacing: 0;
                    line-height: 24px;
                    margin-bottom: 10px;
                    max-width: 626px;
                }
            }
            .form-area {
                .form-item {
                    margin: 60px 0;
                    .entreField-label {
                        display: block;
                        font-family: "Benton Sans", "Helvetica", "sans-serif";
                        font-size: 16px;
                        font-weight: normal;
                        line-height: 26px;
                        letter-spacing: 0;
                        line-height: 24px;
                        min-height: 24px;
                        margin-bottom: 5px;
                    }
                    .entreField-sublabel {
                        font-family: "Benton Sans", "Helvetica", "sans-serif";
                        font-size: 14px;
                        font-weight: normal;
                        line-height: 20px;
                        color: #a8a8a8;
                        margin: 0 0 10px;
                    }
                    .entreField-required {
                        color: #FF324B;
                    }
                    .upper {
                        text-transform: uppercase;
                    }
                    .el-select {
                        width: 80%;
                    }

                }
                .muliti-input {
                    .el-input {
                        width: 180px;
                        margin-right: 15px;
                    }
                }
            }
            .ope-area {
                text-align: right;
                border-top: 1px solid #C8C8C8;
                padding-top: 66px;
                margin-bottom: 66px;
                .el-button--primary {
                    color: #fff;
                    background-color: #7A69B3;
                    border-color: #7A69B3;
                    text-transform: uppercase;
                }
            }
        }
    }
</style>
